﻿using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinChatListen
{
    public class WeChatClientHandler : SimpleChannelInboundHandler<string>
    {

        private event ReceiveMessageEvent receiveMessage;

        public WeChatClientHandler(ReceiveMessageEvent receiveMessage)
        {
            this.receiveMessage = receiveMessage;
        }


        protected override void ChannelRead0(IChannelHandlerContext context, string message)
        {
            Console.WriteLine("Received: " + message);
            if (receiveMessage != null)
                receiveMessage.Invoke(message);
            context.WriteAsync(message);
        }

        public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
        {
            Console.WriteLine("Exception: " + exception);
            context.CloseAsync();
        }

    }
}
